import numpy as np
import matplotlib.pyplot as plt

def manhattan_distance(p1, p2):
    return np.sum(np.abs(p1 - p2))

# 创建数据点
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)

# 选择两个点
p1 = np.array([2, 2])
p2 = np.array([8, 8])

# 计算到p1的距离
Z1 = np.abs(X - p1[0]) + np.abs(Y - p1[1])

# 绘制等高线图
plt.figure(figsize=(10, 8))
plt.contourf(X, Y, Z1, levels=20, cmap='viridis')
plt.colorbar(label='Distance from p1')
plt.plot(p1[0], p1[1], 'ro', markersize=10, label='p1')
plt.plot(p2[0], p2[1], 'bo', markersize=10, label='p2')
plt.plot([p1[0], p2[0], p2[0]], [p1[1], p1[1], p2[1]], 'r--', linewidth=2)
plt.title('Manhattan Distance Visualization')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()

print(f"Manhattan distance between p1 and p2: {manhattan_distance(p1, p2):.2f}")